import numpy as np
from fastapi import APIRouter, Form, UploadFile, File,Request
import os

from starlette.responses import JSONResponse

carInfo = APIRouter()

import os

# 导入opencv库
import cv2
# 导入依赖包
import hyperlpr3 as lpr3

type_list = ["蓝牌", "黄牌单层", "白牌单层", "绿牌新能源", "黑牌港澳", "香港单层", "香港双层", "澳门单层", "澳门双层", "黄牌双层"]
catcher = lpr3.LicensePlateCatcher()


@carInfo.post("/getCarNo",summary="获取车牌")
async def getCarNo(file: UploadFile = File(...)):
    """上传图片进行车牌识别，上传必须为图片类型png/jpg/jpge/wabp"""
    content = await file.read()
    nparr = np.fromstring(content, np.uint8)
    image = cv2.imdecode(nparr, cv2.IMREAD_COLOR).astype(np.uint8)
    plates = catcher(image)
    results = list()
    for code, conf, plate_type, box in plates:
        plate = dict(code=code, conf=float(conf), plate_type=type_list[plate_type], box=box)
        results.append(plate)
    #print(results)
    return JSONResponse({"results": results,'msg':"请求成功"})